import Service from '../../model/service'
import { getEventParam } from '../../utils/utils'
// pages/service-edit/service-edit.js
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		formData: {},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		const service = JSON.parse(options.service)
		this._init(service)
	},
	_init(service) {
		const formData = {
			type: service.type,
			title: service.title,
			category_id: service.category.id,
			description: service.description,
			designated_place: service.designated_place,
			cover_image: service.cover_image,
			begin_date: service.begin_date,
			end_date: service.end_date,
			price: service.price,
		}
		this.setData({
			formData,
			serviceId: service.id,
		})
	},
	async handleSubmit(event) {
		const res = await wx.showModal({
			title: '提示',
			content: '是否确认修改服务？提交后会重新进入审核状态',
			showCancel: true,
		})
		if (!res.confirm) {
			return
		}
		wx.showLoading({
			title: '正在修改....',
			mask: true,
		})
		const formData = getEventParam(event, 'formData')
		try {
			await Service.editService(this.data.serviceId, formData)
			wx.redirectTo({
				url: `/pages/publisher-success/publisher-success?type=${formData.type}`,
			})
		} catch (error) {
			console.log(error)
		}
		wx.hideLoading()
	},
})
